package com.open.party.cloud.view.examine

import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.ResultInfo
import cn.sinothk.hussars.parent.AppEventBusRecycleViewTitleBaseActivity
import com.jiangyy.easydialog.InputDialog
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.ExamineClassMemberCheckVo
import com.open.party.cloud.model.ExamineClassMemberEntity
import com.open.party.cloud.view.examine.adapter.ExamineClassMemberCheckListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_app_bg.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineClassMemberCheckListActivity : AppEventBusRecycleViewTitleBaseActivity<String>(), OnSuperListener<ExamineClassMemberEntity> {
    var bizViewModel: BizViewModel? = null
    var viewModel: ExamineViewModel? = null
    var classId: String? = null

    private var adapter: ExamineClassMemberCheckListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_app_bg

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        classId = intent.getStringExtra("classId")
        setViewTitle("入班申请")
        viewModel = ExamineViewModel()
        bizViewModel= BizViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = ExamineClassMemberCheckListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            refresh()
        }
        refresh()
    }

    private fun refresh() {
        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<String>) {

//        val pageReq = PageReq<ExamineClassVO>()
//        pageReq.pageNo = pageVo.pageNum
//        pageReq.pageSize = 10
//
//        val vo = ExamineClassVO()
//        vo.classId = classId
//        pageReq.data = vo

//        viewModel!!.getExamineClassMemberPage(pageReq)
        viewModel!!.schoolUserGetAuditList(classId)

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<List<ExamineClassMemberEntity>>) {
        if (result.eventType != "schoolUserGetAuditList") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data.isEmpty()) {
                    loadingTipView.showEmpty("暂无人员", R.drawable.empty_nodata)
                    loadingRecyclerView.setLoadingMoreEnabled(false)
                } else {
                    adapter!!.setData(result.data)
                    loadingTipView.showContent()
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<ExamineClassMemberEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    var id: String? = null
    override fun onClick(position: Int, itemData: ExamineClassMemberEntity, flag: String?) {
        id = itemData.id
        when (flag) {
            "backBtn" -> {
                InputDialog.Builder(this)
                    .setTitle("拒绝加入")
                    .setHint("请输入拒绝理由")
                    .setLines(2)
                    .setPositiveButton("确定") { view ->
                        val contentStr = view.tag.toString()
                        if (XUtils.string().isEmpty(contentStr)) {
                            XUtils.toast().show("请输入拒绝理由")
                        }else{
                            submit("back", contentStr)
                        }
                    }
                    .setNegativeButton("取消", null)
                    .show()
            }
            "passBtn" -> {
                submit("pass", "审批通过")
            }
        }
    }

    private fun submit(state: String, contentStr: String) {
        val vo = ExamineClassMemberCheckVo();

        if (!XUtils.net().isAvailable) {
            XUtils.toast().show("当前网络不可用")
            return
        }

        vo.id = id
        vo.state = state
        vo.remark = contentStr

        showLoadingDialog("提交中")
        bizViewModel!!.submitClassMemberCheck(vo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusSubmitCallback(result: ResultInfo<AppPageData<ArticleEntity>>) {
        if (result.eventType != "submitClassMemberCheck") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    XUtils.toast().show("提交成功")
                    refresh()
                } else {
                    XUtils.toast().show("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}